---
title: "Appendix A for 'Breaking Down Pillars of Support for Democratic Backsliding'"
subtitle: "Additional Tables and Figures"
author: "Jonathan Pinckney and Claire Trilling"
date: "`r Sys.Date()`"
output: 
  bookdown::pdf_document2:
    toc: FALSE
    extra_dependencies: ["float","flafter"]
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F,fig.pos = "H")

library(tidyverse)
library(marginaleffects)
library(modelsummary)
library(sandwich)
library(lmtest)
library(kableExtra)
library(texreg)
library(broom)

analysis.data <- read_rds("data/analysis-datasets.rds")

spell.level.data <- analysis.data[[1]]
tactic.level.data <- analysis.data[[2]]
loyalty.shift.data <- analysis.data[[3]]

# Set theme colors

theme.blue <- "#003C7F"
theme.green <- "#47A035"
```

```{r all-spells}
spell.level.data %>%
  select(country,start_year, end_year,spell_outcome) %>%
  rename(Country = country,`Starting Year` = start_year,
         `Ending Year` = end_year,Outcome = spell_outcome) %>% 
  arrange(Country) %>%
  kable(format = "latex",
        booktabs = T,
        caption = "Full Set of Democratic Backsliding Spells")
```

\pagebreak

# Additional Descriptive Tables and Figures {-}

Table \@ref(tab:spell-sum) includes summary statistics on all the variables included in our regression model of the impact of civil resistance on the protection of democracy. Our measures of civil resistance and the protection of democracy come from our own data collection and are described in the main text. 

Four of our five control variables came from V-Dem. Our measure of presidentialism is the Presidentialism Index (v2xnp_pres) from V-Dem, which is constructed through a Bayesian factor analysis of ten indicators of legislative and judicial restraints on the executive. See pages 300-301 of the V-Dem codebook for more information. Our measure of media freedom is the Freedom of Expression and Alternative Sources of Information index from V-Dem (v2x_freexp_altinf), which is constructed using a Bayesian factor analysis of eight indicators of media freedom. Our measure of polarization is the "Political Polarization" (v2cacamps) variable from V-Dem, which is a single indicator in which multiple country experts answered the question "Is society polarized into antagonisic political camps?" on a 5-point ordinal scale and their answers were combined into a single indicator using V-Dem's Bayesian item response theory measurement model (See page 232 of the V-Dem codebook). Our measure of democracy at the beginning of a backsliding spell was the V-Dem Polyarchy score (v2x_polyarchy), which we describe in the main text. 
All V-Dem indicators are given at the country-year level, while some of our backsliding spells continue for multiple years. Thus, to generate our spell-level indicators we averaged the V-Dem indicators over all the years included in our spells.
We drew our measure of recessions from the World Bank's World Development Indicators. We first took the World Bank's measure of GDP per capita (NY.GDP.PCAP.CD), and then calculated whether there was a drop in GDP per capita in any year included in our backsliding spells. If such a drop occurred we coded the variable as a 1, otherwise as a 0.

```{r spell-sum, message=FALSE}
spell.level.data %>% 
  select(cr,prez,med.free,polar,dem.strt,rcsn,end.dem) %>% 
  mutate(cr = if_else(cr == "yes",1,0)) %>% 
  pivot_longer(cols = everything(),names_to = "Variable") %>%
  mutate(Variable = case_match(Variable,
                               "cr" ~ "Civil Resistance",
                               "prez" ~ "Presidentialism",
                               "med.free" ~ "Media Freedom",
                               "polar" ~ "Polarization",
                               "dem.strt" ~ "Starting Democracy Level",
                               "rcsn" ~ "Recession",
                               "end.dem" ~ "Democracy Protected")) %>% 
  group_by(Variable) %>% 
  summarize(Mean = mean(value),
            `Std. Dev` = sd(value),
            Min = min(value),
            Max = max(value)) %>% 
  kable(format = "latex",
        booktabs = T,
        caption = "Summary Statistics on Spell-Level Data")

```

Table \@ref(tab:tactic-freq) shows the frequencies of pillar-tactic combinations in our tactic-level data. As noted in the main text, we observed tactics by civic and faith actors much more frequently than tactics by business or security forces/veterans, and observed physical and verbal protest more frequently than other tactic categories.  

```{r tactic-freq, message=FALSE}
tactic.level.data %>% 
  group_by(tactic_category,pillar) %>% 
  summarize(count = n()) %>% 
  pivot_wider(names_from = pillar,values_from = count) %>% 
  mutate(across(1:4, ~ replace_na(.,0))) %>%
  rename(Tactic = tactic_category) %>% 
  mutate(Tactic = case_match(Tactic,
                             "bridgebuilding" ~ "Outreach",
                             "institutional action" ~ "Institutional Action",
                             "campaign material support" ~ "Material Support",
                             "protest (verbal)" ~ "Verbal Protest",
                             "protest (physical)" ~ "Physical Protest",
                             "intervention" ~ "Nonviolent Intervention",
                             "noncooperation" ~ "Non-Cooperation")) %>% 
  kable(format = "latex",
        booktabs = T,
        caption = "Tactic/Pillar Combination Frequencies")
```
Figure \@ref(fig:tactic-success) reports success rates across pillar-tactic combinations. Note that empty columns with 0% over them indicates that we did observe that pillar-tactic combination, but that none of those tactics were successful, while fully empty columns indicate that we did not observe any instances of that pillar-tactic combination.

```{r tactic-success, fig.align='center',fig.cap="Tactic Success Rates Across Pillars",message=FALSE}
tactic.level.data %>% 
  filter(!(is.na(tactic_category))) %>% 
  group_by(tactic_category,pillar) %>% 
  summarize(success.rate = mean(success,na.rm = T),
            count = n()) %>%
  ggplot(aes(x = tactic_category,
             y = success.rate)) +
  geom_col() +
  geom_text(aes(label = scales::percent(success.rate,accuracy = 1)),vjust = -0.5) +
  scale_y_continuous(name = "Success Rate",
                     labels = scales::percent,limits = c(0,1.17),
                     breaks = c(0,0.3,0.6,0.9)) +
  scale_x_discrete(name = "",labels = c("Outreach","Institutional\nAction","Material\nSupport","Verbal\nProtest","Physical\nProtest","Intervention","Non-Cooperation")) +
  facet_wrap(. ~ pillar) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45,hjust = 1),
        panel.grid.minor = element_blank())

```

# Regression Tables {-}

Table \@ref(tab:loyalty-shift-reg) reports full regression results from two models of pillar alignment at the end of a backsliding spell, interacting outreach strategies with pillar alignment at the beginning of the spell and controlling for pillar. In both cases the dependent variable is our five-level Likert scale of pillar alignment, ranging from "completely support regime" to "completely support democracy." In the linear model, this is simply transformed to a 1-5 numeric scale. In the ordered logit model this is transformed to a factor variable ordered from "completely support regime" to "completely support democracy." Note that in the ordered logit there are separate intercepts for the first four levels of the dependent variable. Table \@ref(tab:tactic-success-reg) contains regression output from the logistic regression modeling the impact of noncooperation tactics interacted with pillar on tactic-level success. Note that the business pillar was used as the baseline pillar category.

```{r loyalty-shift-reg, results='asis'}
linear.defections.mod <- lm(align_end_num ~ outreach*align_begin_num + pillar, # Run linear model
                            data = loyalty.shift.data) 

ologit.alignment.mod <- MASS::polr(align_end ~ outreach*align_begin_num + pillar, # Run Ologit model
                               data = loyalty.shift.data,
                               Hess = T) 

linear.clustered.ses <- coeftest(linear.defections.mod, # Get clustered SEs and p-values
         vcov = vcovCL,
         type = "HC1",
         cluster = ~case_id)

ologit.clustered.ses <- coeftest(ologit.alignment.mod, # Get clustered SEs and p-values
         vcov = vcovCL,
         type = "HC1",
         cluster = ~case_id)

# Coeftest does not return clustered standard errors for DV-level intercepts, 
# so using original standard errors and p-values for these
ologit.tidied <- tidy(ologit.alignment.mod) %>% 
  mutate(pvals = 2 * pt(abs(statistic), 74, lower.tail = FALSE))

ologit.ses <- c(ologit.clustered.ses[,2],ologit.tidied$std.error[7:10])
ologit.pvals <- c(ologit.clustered.ses[,4],ologit.tidied$pvals[7:10])



texreg(list(linear.defections.mod,ologit.alignment.mod),
       override.se = list(linear.clustered.ses[,2],ologit.ses), # Replace with clustered SEs
       override.pvalues = list(linear.clustered.ses[,4],ologit.pvals), # Replace with clustered p-values
       custom.coef.map = list("outreach" = "Outreach",
                              "align_begin_num" = "Beginning Alignment",
                              "outreach:align_begin_num" = "Outreach*Beginning Alignment",
                              "pillarcivic" = "Civic Groups",
                              "pillarfaith" = "Faith Communities",
                              "pillarvetsec" = "Vets/Security Forces",
                              "(Intercept)" = "Constant",
                              "completely support regime|mostly support regime" = "Completely Support Regime",
                              "mostly support regime|neutral or mixed" = "Mostly Support Regime",
                              "neutral or mixed|mostly support democracy" = "Neutral or Mixed",
                              "mostly support democracy|completely support democracy" = "Mostly Support Democracy"),
       groups = list("Intercepts" = 7:11),
       custom.model.names = c("Linear","Ordered Logit"),
       caption = "Outreach and Pillar Alignment Models",
       caption.above = T,
       label = "tab:loyalty-shift-reg",
       custom.note = "%stars. Clustered SEs in parentheses."
       )


```

```{r tactic-success-reg,results='asis'}
tactic.success.mod <- glm(success ~ noncooperation*pillar + 
                            v2x_polyarchy + v2cademmob +
                            country_name + as.factor(start_year),
                          family = binomial(link = "logit"), 
                          data = tactic.level.data) 

tactic.clustered.ses <- coeftest(tactic.success.mod, # Cluster standard errors
         vcov = vcovCL,
         type = "HC1",
         cluster = ~case_id)

texreg(tactic.success.mod,
    custom.coef.map = list("noncooperationYes" = "Non-Cooperation",
                              "pillarCivic Groups" = "Civic Groups",
                              "pillarFaith Communities" = "Faith Communities",
                              "pillarVeterans and Sec. Forces" = "Veterans and Sec. Forces",
                              "v2x_polyarchy" = "Polyarchy",
                              "v2cademmob" = "Democratic Mobilization",
                              "noncooperationYes:pillarCivic Groups" = "Non-Cooperation*Civic",
                              "noncooperationYes:pillarFaith Communities" = "Non-Cooperation*Faith",
                              "noncooperationYes:pillarVeterans and Sec. Forces" = "Non-Cooperation*Vets",
                           "(Intercept)" = "Intercept"),
    override.se = tactic.clustered.ses[,2],
    override.pvalues = tactic.clustered.ses[,4],
    custom.gof.rows = list("Country FE" = "Yes","Year FE" = "Yes"),
    custom.note = "\\parbox{.4\\linewidth}{\\vspace{2pt}%stars. \\\\ Business pillar used as baseline. \\\\ Clustered SEs in parentheses.}",
    caption = "Noncooperation Success Across Pillars",
    caption.above = TRUE,
    label = "tab:tactic-success-reg")
```



